//
//  YTETAddrController.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/9/11.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTETAddrController.h"
#import "SGPagingView.h"
#import "YTPorpertyAddrController.h"
#import "YTaddCollectAddrController.h"
#import "YTWalletAddrController.h"

@interface YTETAddrController ()<SGPageContentScrollViewDelegate, SGPageTitleViewDelegate>
@property(nonatomic ,strong)SGPageTitleView *pageTitleView;

@property(nonatomic ,strong)SGPageContentScrollView *pageContentScrollView;

@property(nonatomic ,strong)UIButton *addAdrrButton;

@property(nonatomic ,assign)BOOL isCollectAddr;
@end

@implementation YTETAddrController

- (UIButton *)addAdrrButton
{
    if (!_addAdrrButton) {
        
        CGFloat buttonY = CGRectGetMaxY(self.pageContentScrollView.frame);
        _addAdrrButton = [[UIButton alloc] initWithFrame:CGRectMake(0, buttonY, kScreenWidth, 44)];
        [_addAdrrButton setTitle:@"新增地址" forState:UIControlStateNormal];
        [_addAdrrButton setBackgroundColor:UIColor.redColor];
        [_addAdrrButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [_addAdrrButton addTarget:self action:@selector(addAddrBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addAdrrButton;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self setupNav];

}

#pragma mark: - 新增地址按钮监听
- (void)addAddrBtnClick
{
    if (self.isCollectAddr) {
        YTaddCollectAddrController *vc = [[YTaddCollectAddrController alloc] init];
        [self.navigationController pushViewController:vc animated:true];
    }else{
        YTWalletAddrController *vc = [[YTWalletAddrController alloc] init];
        [self.navigationController pushViewController:vc animated:true];
    }
}

- (void)setupUI
{
    NSArray *nameList = @[@"钱包地址" , @"收货地址"];
    SGPageTitleViewConfigure *configure = [SGPageTitleViewConfigure pageTitleViewConfigure];
    configure.showVerticalSeparator = YES;
    configure.verticalSeparatorColor = UIColor.lightGrayColor;
    
    
    /// pageTitleView
    self.pageTitleView = [SGPageTitleView pageTitleViewWithFrame:CGRectMake(0,0, kScreenWidth, 34)
                                                        delegate:self
                                                      titleNames:nameList
                                                       configure:configure];
    [self.view addSubview:self.pageTitleView];
    
    NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:5];
    for (int i = 0; i < nameList.count; i++) {
        YTPorpertyAddrController *vc = [[YTPorpertyAddrController alloc] init];
        vc.isCollectAddr = i;
        [arrayM addObject:vc];
    }
    

    
    /// pageContent
    CGFloat pageContentViewY = CGRectGetMaxY(self.pageTitleView.frame);
    self.pageContentScrollView = [[SGPageContentScrollView alloc] initWithFrame:CGRectMake(0,
                                                                                           pageContentViewY,
                                                                                           kScreenWidth,
                                                                                           kScreenHeight - pageContentViewY - (KIsiPhoneX ? 88 : 64) - 44)
                                                                       parentVC:self
                                                                       childVCs:arrayM.copy];
    self.pageContentScrollView.delegatePageContentScrollView = self;
    [self.view addSubview:self.pageContentScrollView];
    [self.view addSubview:self.addAdrrButton];

    self.view.backgroundColor = UIColor.whiteColor;
    
}

- (void)setupNav
{
    [self.navigationController.navigationBar setTintColor:UIColor.blackColor];

    self.title = @"地址管理";
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"地址管理";
    titleLabel.font = [UIFont boldSystemFontOfSize:16];
    titleLabel.textColor = UIColor.blackColor;
    self.navigationItem.titleView = titleLabel;
    
}


#pragma mark: - SGPageContentScrollViewDelegate, SGPageTitleViewDelegate
- (void)pageTitleView:(SGPageTitleView *)pageTitleView
        selectedIndex:(NSInteger)selectedIndex
{
    [self.pageContentScrollView setPageContentScrollViewCurrentIndex:selectedIndex];
    
}
- (void)pageContentScrollView:(SGPageContentScrollView *)pageContentScrollView
                     progress:(CGFloat)progress
                originalIndex:(NSInteger)originalIndex
                  targetIndex:(NSInteger)targetIndex
{
    [self.pageTitleView setPageTitleViewWithProgress:progress originalIndex:originalIndex targetIndex:targetIndex];
    
}

- (void)pageContentScrollView:(SGPageContentScrollView *)pageContentScrollView index:(NSInteger)index
{
    self.isCollectAddr = index;
}

@end
